<template>
	<view class="hyzb">
		<view class="right">
			<view class="right_top">
				<u-input
					class="u-input"
					v-model="query" 
					type="select" 
					:border="true" 
					@click="levelShow = true" 
					:custom-style="{
						width:'65px',
						marginRight:'0',
					}" />
				<u-action-sheet
					class="u-action-sheet"
					:z-index="99999"
					:list="actionSheetList2" 
					v-model="levelShow" 
					@click="actionSheetCallback">
				</u-action-sheet>
			</view>
			<view class="right_bottom">
				<qiun-data-charts
					type="column" 
					:chartData="chartData"
					:opts="opts"
					@getIndex="chartClick"
					:ontouch="true"
					:canvas2d="true"
					canvasId="hyzb_charts"/>
			</view>
		</view>
	</view>
</template>

<script>
	import{
		doubleClick,
	} from "@/common/OtherTools";
	export default { //行业占比组件
		name: "HyzbCp",
		data() {
			return {
				barList: [],
				query: '责任单位',
				levelShow: false,
				actionSheetList2: [
					{
						text: '责任单位'
					},
					{
						text: '分管领导'
					},
					{
						text: '行业'
					},
					{
						text: '月度汇总'
					},
				],
				right_canvas: {
					height: null,
					width: null,
				},
				chartXList:[],  //图表的x轴数据
				chartData:{},
				opts:{
					"type": "column",
					"canvasId": "",
					"canvas2d": true,
					"background": "none",
					"animation": true,
					"timing": "easeOut",
					"duration": 1000,
					"color": [
						"#1890FF",
						"#91CB74",
						"#FAC858",
						"#EE6666",
						"#73C0DE",
						"#3CA272",
						"#FC8452",
						"#9A60B4",
						"#ea7ccc"
					],
					"padding": [
						15,
						15,
						0,
						15
					],
					"rotate": false,
					"errorReload": true,
					"fontSize": 10,
					"fontColor": "#666666",
					"enableScroll": true,
					"touchMoveLimit": 60,
					"enableMarkLine": false,
					"dataLabel": true,
					"dataPointShape": true,
					"dataPointShapeType": "solid",
					"xAxis": {
						"disabled": false,
						"axisLine": false,
						"axisLineColor": "#CCCCCC",
						"calibration": false,
						"fontColor": "#666666",
						"fontSize": 10,
						"rotateLabel": false,
						"itemCount": 3,
						"boundaryGap": "center",
						"disableGrid": true,
						"gridColor": "#CCCCCC",
						"gridType": "solid",
						"dashLength": 4,
						"gridEval": 1,
						"scrollShow": false,
						"scrollAlign": "left",
						"scrollColor": "#A6A6A6",
						"scrollBackgroundColor": "#EFEBEF",
					},
					"yAxis": {
						"disabled": true,
						"disableGrid": true,
						"splitNumber": 5,
						"gridType": "solid",
						"dashLength": 8,
						"gridColor": "#CCCCCC",
						"padding": 10,
						"showTitle": false,
						"data": []
					},
					"legend": {
						"show": true,
						"position": "bottom",
						"float": "center",
						"padding": 5,
						"margin": 5,
						"backgroundColor": "rgba(0,0,0,0)",
						"borderColor": "rgba(0,0,0,0)",
						"borderWidth": 0,
						"fontSize": 13,
						"fontColor": "#666666",
						"lineHeight": 11,
						"hiddenColor": "#CECECE",
						"itemGap": 10
					},
					"extra": {
						"column": {
							"type": "group",
							"width": 15,
							"seriesGap": 2,
							"categoryGap": 3,
							"barBorderCircle": false,
							"linearType": "none",
							"linearOpacity": 1,
							"colorStop": 0,
							"meterBorder": 1,
							"meterFillColor": "#FFFFFF",
							"activeBgColor": "#000000",
							"activeBgOpacity": 0.08,
							"meterBorde": 1
						},
						"tooltip": {
							"showBox": true,
							"showArrow": true,
							"showCategory": true,
							"borderWidth": 0,
							"borderRadius": 0,
							"borderColor": "#000000",
							"borderOpacity": 0.7,
							"bgColor": "#000000",
							"bgOpacity": 0.7,
							"gridType": "solid",
							"dashLength": 4,
							"gridColor": "#CCCCCC",
							"fontColor": "#FFFFFF",
							"splitLine": true,
							"horizentalLine": false,
							"xAxisLabel": false,
							"yAxisLabel": false,
							"labelBgColor": "#FFFFFF",
							"labelBgOpacity": 0.7,
							"labelFontColor": "#666666"
						},
						"markLine": {
							"type": "solid",
							"dashLength": 4,
							"data": []
						}
					}
				},
			}
		},
		mounted() {
			this.getDataBar();
		},
		methods: {
			chartClick:doubleClick(function(param){  //表格双击事件
				let key,name;
				switch(this.query){
					case '责任单位':
						key = 'company';
						name = this.chartXList[param.currentIndex.index];
						if(!name) return;
						uni.navigateTo({
						    url: `/pages/project_gk/project_gk?type=${key}&name=${name}`,
						});
						break;
					case '分管领导':
						key = 'companyLeader';
						name = this.chartXList[param.currentIndex.index];
						if(!name) return;
						uni.navigateTo({
						    url: `/pages/project_gk/project_gk?type=${key}&name=${name}`,
						});
						break;
					case '行业':
						key = 'industry';
						name = this.chartXList[param.currentIndex.index];
						if(!name) return;
						uni.navigateTo({
						    url: `/pages/project_gk/project_gk?type=${key}&name=${name}`,
						});
						break;
					case '月度汇总':
						name = this.chartXList[param.currentIndex.index];
						if(!name) return;
						uni.navigateTo({
						    url: `/pages/summery/index?type=${name}`,
						});
						break;
				}
			}),
			getDataBar() {
				this.$ajax({
					url: '/wx/index/bar',
					method: 'GET',
					data: {
						query: this.query,
					},
					success: res => {
						this.barList = res.data.data;
						switch(true){
							case this.query == '分管领导':
								this.sortLeader(this.barList);
								break;
							default:
								setTimeout(res =>{
									this.drawColumn(this.barList);
								},500);
						}
					}
				})
			},
			// 领导筛选时排序
			sortLeader(data){
				let barData = []
				let leaderName = [
					'刘会英',
					'邹远骏',
					'刘会英',
					'白光裕',
					'张胜虎',
					'江涛',
					'王小武',
					'王英',
					'宋联忠',
					'孙武',
					'汪旭',
					'赵旭阳',
					'蒋欣',
					'胡勇',
					'张平富',
					'李文',
					'陈志光',
					'屈志新',
					'周红英',
					'陈易',
					'彭俊',
					'唐昌金',
					'古明水',
					'高晓梅',
					'刘锵',
					'俞斌',
					'吴治全',
					'章谷雅',
					'廖正林',
					'邓启明',
				]
				leaderName.forEach(item =>{
					for(let i=data.length-1;i >= 0; i--){
						if(data[i] && item === data[i].name){
							barData.push(data[i])
							data.splice(i,1)
						}
					}
					
				})
				barData = [
					...barData,
					...data
				]
				setTimeout(res =>{
					this.drawColumn(barData);
				},500)	
			},
			drawColumn(chartData) {  //绘制图表
				let [arr,data,data1,data2] = [[],[],[],[]];
				switch(true){
					case this.query == '月度汇总':
						chartData.forEach((item, index) => {
							arr.push(item.name);
							data.push(item.data);
						})
						this.chartXList = arr;
						this.chartData = {
							categories:arr,
							series: [
								{
									"name": "月度汇总",
									"data": data,
									"color": '#1890FF',
									format:(item) =>{
										if(item == 0){
											return ''
										}else{
											return item
										}
									}
								},
							],
						};
						break;
					default:
						chartData.forEach((item, index) => {
							arr.push(item.name);
							data.push(item['已完成']);
							data1.push(item['在建']);
							data2.push(item['超时']);
						});
						this.chartXList = arr;
						this.chartData = {
							categories:arr,
							series: [
								{
									"name": "在建",
									"data": data1,
									"color": '#1890FF',
									format:(item) =>{
										if(item == 0){
											return ''
										}else{
											return item
										}
									}
								},
								{
									"name": "完工",
									"data": data,
									"color": '#2FC25B',
									format:(item) =>{
										if(item == 0){
											return ''
										}else{
											return item
										}
									}

								},
								{
									"name": "超时",
									"data": data2,
									"color": '#f40',
									format:(item) =>{
										if(item == 0){
											return ''
										}else{
											return item
										}
									}
								},
							],
						};
				}
			},
			actionSheetCallback(index) {
				this.query = this.actionSheetList2[index].text;
				this.getDataBar()
			},
		},
	}
</script>

<style lang="scss" scoped>
	@mixin flex_layout_row {
		//水平布局
		display: flex;
		flex-direction: row;
	}

	@mixin flex_layout_column {
		//垂直布局
		display: flex;
		flex-direction: column;
	}

	.hyzb {
		width: 100%;
		height: fit-content;
		background-color: #ffffff;
		border-radius: 5px;
		@include flex_layout_row;
		align-items: stretch;
		box-sizing: border-box;
		overflow: hidden;

		.charts {
			width: 100%;
			height: 100%;
			background-color: #FFFFFF;
		}
		.right {
			flex: 1;
			width: 0;
			@include flex_layout_column;
			justify-content: stretch;
			align-items: flex-end;
			padding: 10px;
			box-sizing: border-box;
			height: fit-content;
			.right_top {
				width: 100%;
				@include flex_layout_row;
				justify-content: flex-end;
				align-items: center;
				margin-bottom: 10px;
				.u-input{
					width: 100%;
				}
			}

			.right_bottom {
				width: 100%;
				height: 200px;
				flex: 1;
				border-radius: 5px;
			}
		}
	}
</style>
